package com.etime.shuhuiyuan.controllers.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.User;
import com.etime.shuhuiyuan.services.user.interfaces.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * user控制层
 */
@Api(tags = "user案例接口")
@RestController
@RequestMapping("/user")
//@ResponseBody
public class UserController {
    /**
     * 自动注入实例
     */
    @Autowired
    private UserService userService;

    @ApiOperation("添加user")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userid",value = "自增长主键，需要传递"),
            @ApiImplicitParam(name = "username",value = "传递字符串"),
            @ApiImplicitParam(name = "userpwd",value = "传递字符串"),
            @ApiImplicitParam(name = "usertruename",value = "传递字符串"),
            @ApiImplicitParam(name = "userbirthyear",value = "传递数字"),
            @ApiImplicitParam(name = "userbirthmonth",value = "传递数字"),
            @ApiImplicitParam(name = "userbirthday",value = "传递数字"),
            @ApiImplicitParam(name = "useroccupation",value = "传递字符串"),
            @ApiImplicitParam(name = "usermajor",value = "传递字符串"),
            @ApiImplicitParam(name = "userphone",value = "传递字符串"),
            @ApiImplicitParam(name = "useremail",value = "传递字符串"),
            @ApiImplicitParam(name = "userstate",value = "传递数字"),
            @ApiImplicitParam(name = "showpic",value = "传递字符串"),
            @ApiImplicitParam(name = "isteacher",value = "传递数字"),
            @ApiImplicitParam(name = "issaler",value = "传递数字"),
            @ApiImplicitParam(name = "nickname",value = "传递字符串"),
            @ApiImplicitParam(name = "openid",value = "传递字符串"),
            @ApiImplicitParam(name = "unionid",value = "传递字符串"),
            @ApiImplicitParam(name = "code",value = "传递字符串"),
            @ApiImplicitParam(name = "createtime",value = "传递时间"),
            @ApiImplicitParam(name = "saleruserid",value = "传递数字"),
            @ApiImplicitParam(name = "edusohoid",value = "传递数字")
    })
    @PostMapping("/insert")
    public BaseMessage insertUser(User user){
        BaseMessage msg=new BaseMessage();
        int rtn=userService.insertUser(user);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("添加user成功");
        }else {
            msg.setMsg("添加user失败");
        }
        return msg;
    }
    @ApiOperation("修改user")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid",value = "自增长主键，需要传递"),
            @ApiImplicitParam(name = "username",value = "传递字符串"),
            @ApiImplicitParam(name = "userpwd",value = "传递字符串"),
            @ApiImplicitParam(name = "usertruename",value = "传递字符串"),
            @ApiImplicitParam(name = "userbirthyear",value = "传递数字"),
            @ApiImplicitParam(name = "userbirthmonth",value = "传递数字"),
            @ApiImplicitParam(name = "userbirthday",value = "传递数字"),
            @ApiImplicitParam(name = "useroccupation",value = "传递字符串"),
            @ApiImplicitParam(name = "usermajor",value = "传递字符串"),
            @ApiImplicitParam(name = "userphone",value = "传递字符串"),
            @ApiImplicitParam(name = "useremail",value = "传递字符串"),
            @ApiImplicitParam(name = "userstate",value = "传递数字"),
            @ApiImplicitParam(name = "showpic",value = "传递字符串"),
            @ApiImplicitParam(name = "isteacher",value = "传递数字"),
            @ApiImplicitParam(name = "issaler",value = "传递数字"),
            @ApiImplicitParam(name = "nickname",value = "传递字符串"),
            @ApiImplicitParam(name = "openid",value = "传递字符串"),
            @ApiImplicitParam(name = "unionid",value = "传递字符串"),
            @ApiImplicitParam(name = "code",value = "传递字符串"),
            @ApiImplicitParam(name = "createtime",value = "传递时间"),
            @ApiImplicitParam(name = "saleruserid",value = "传递数字"),
            @ApiImplicitParam(name = "edusohoid",value = "传递数字")
    })
    @PostMapping("/update")
    public BaseMessage updateUser(User user){
        BaseMessage msg=new BaseMessage();
        int rtn=userService.updataUser(user);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("修改user成功");
        }else {
            msg.setMsg("修改user失败");
        }
        return msg;
    }
    @ApiOperation("查询user列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "传递字符串"),
            @ApiImplicitParam(name = "userstate",value = "传递数字"),
    })
    @GetMapping("/list")
    public ListMessage<User> selectUserList(User user){//查询user列表方法
        ListMessage<User> listMessage=new ListMessage<>();//创建类的对象
        List<User> list=userService.selectUserList(user);//业务逻辑层查询user的列表
        if(list!=null){
            listMessage.setDatalist(list);//将查询到的user列表存入listMessage中
            listMessage.setCode(1);
        }else {
            listMessage.setCode(0);
            listMessage.setMsg("没有查询到数据");
        }
        return listMessage;//返回值给ListMessage公共类
    }
    @ApiOperation("分页查询user列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "传递字符串"),
            @ApiImplicitParam(name = "userstate",value = "传递数字"),
            @ApiImplicitParam(name = "pagnum",value = "第几页"),
            @ApiImplicitParam(name = "pagsize",value = "每页行数")
    })
    @GetMapping("/page")
    public PageMessage<User> selectUserListByPage(User user, Long pagenum, Long pagesize){
        PageMessage<User> msg=new PageMessage<>();
        Page<User> pagedate=userService.selectUserByPage(user,pagenum,pagesize);
        if(pagedate!=null){
            msg.setPagedata(pagedate);
            msg.setCode(1);
        }else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }
    @ApiOperation("根据id查询user")
    @ApiImplicitParam("userid")
    @GetMapping("/get/{userid}")
    public PojoMessage<User> selectUserListById(@PathVariable("userid") int userid){
        PojoMessage<User> msg=new PojoMessage<>();
        User user=userService.selectUserById(userid);
        if(user!=null){
            msg.setData(user);
            msg.setCode(1);
        }else {
            msg.setCode(0);
            msg.setMsg("没有查询到指定数据");
        }
        return msg;
    }
}